package com.tenglong.teacher.ui.homework;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.cuotibao.zhuoxiao.basics.GlobalConstant;
import com.netease.nimlib.sdk.msg.attachment.FileAttachment;
import com.netease.nimlib.sdk.msg.attachment.ImageAttachment;
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.HomeWorkIM;
import com.tenglong.teacher.ui.PhotoBrowserActivity;
import com.tenglong.teacher.ui.common.view.TakePicLayout;
import com.tenglong.teacher.ui.homework.msgviewholder.HWAudioViewHolder;
import com.tenglong.teacher.ui.homework.msgviewholder.HWBaseViewHolder;
import com.tenglong.teacher.ui.homework.msgviewholder.HWPicViewHolder;
import com.tenglong.teacher.ui.homework.msgviewholder.HWTitleViewHolder;
import com.tenglong.teacher.ui.homework.msgviewholder.HWTxtViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wim on 4/14/16.
 */
public class HomeWorkDetailAdapter extends RecyclerView.Adapter<HWBaseViewHolder> implements TakePicLayout.OnCloseClickListener {

    private List<IMMessage> singleList;
    public final static int TITLE_TYPE=1;
    public final static int TITLE_TEXT=2;
    public final static int TITLE_PIC=3;
    public final static int TITLE_AUDIO=4;
    public final static int GRIDSPAN_IMAGE=3;
    public HomeWorkDetaiListener listeners;

    public HomeWorkDetailAdapter() {
        singleList = new ArrayList<>();
    }

    public void add(IMMessage item) {
        singleList.add(item);
        notifyItemInserted(singleList.size() - 1);
    }
    public List<IMMessage> getSingleList(){
        return singleList;
    }
    public void addAll(List<IMMessage> singleList) {
        for (IMMessage single : singleList) {
            add(single);
        }
    }

    public void remove(IMMessage item) {
        int position = singleList.indexOf(item);
        if (position > -1) {
            singleList.remove(position);
            notifyItemRemoved(position);
        }
    }

    public void clear() {
        while (getItemCount() > 0) {
            remove(getItem(0));
        }
    }

    public IMMessage getItem(int position){
        return singleList.get(position);
    }

    @Override
    public int getItemViewType(int position) {
        if(singleList.get(position).getMsgType()==MsgTypeEnum.custom){
            return TITLE_TYPE;
        }else  if(singleList.get(position).getMsgType()==MsgTypeEnum.text){
            return TITLE_TEXT;
        }else  if(singleList.get(position).getMsgType()==MsgTypeEnum.image){
            return TITLE_PIC;
        }else if(singleList.get(position).getMsgType()==MsgTypeEnum.audio){
            return TITLE_AUDIO;
        }
        return 0;

    }
    public int getSpanSize(int position) {
        int count;
        if (getItemViewType(position)==TITLE_PIC) {
            count = 1;
        } else {
            count = GRIDSPAN_IMAGE;
        }
        return count;
    }


    @Override
    public HWBaseViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch(viewType){
            case TITLE_TYPE:
                return new HWTitleViewHolder(inflater.inflate(R.layout.hw_item_title, parent, false));
            case TITLE_TEXT:
                return new HWTxtViewHolder(inflater.inflate(R.layout.hw_item_txt, parent, false));
            case TITLE_PIC:
                return new HWPicViewHolder(inflater.inflate(R.layout.hw_item_pic, parent, false), new HomeWorkDetaiListener() {
                    @Override
                    public void onPhotoListener(IMMessage message) {
                        ArrayList<String> stringList=new ArrayList<>();
                        for(IMMessage imMessage: singleList){
                            if(message.getFromAccount().equals(imMessage.getFromAccount())&&
                                    message.getMsgType().equals(imMessage.getMsgType())&&message.getDirect()==
                                    imMessage.getDirect()&&((HomeWorkIM)message).getType()==((HomeWorkIM)imMessage).getType()){
                                FileAttachment msgAttachment = (FileAttachment) imMessage.getAttachment();
                                String path = msgAttachment.getPath();
                                String thumbPath = msgAttachment.getThumbPath();
                                String thumbUrl = null;
                                if (msgAttachment instanceof ImageAttachment) {
                                    thumbUrl = ((ImageAttachment) msgAttachment).getThumbUrl();
                                }
                                String url = msgAttachment.getUrl();
                                if (!TextUtils.isEmpty(thumbPath)) {
                                    stringList.add(thumbPath);
                                } else if (!TextUtils.isEmpty(path)) {
                                    stringList.add(path);
                                } else if (!TextUtils.isEmpty(thumbUrl)) {
                                    stringList.add(thumbUrl);
                                } else if (!TextUtils.isEmpty(url)) {
                                    stringList.add(url);
                                }
                            }
                        }
                        Intent intent = new Intent();
                        intent.putExtra(GlobalConstant.KEY_IMAGE_URLS, stringList.toArray(new String[stringList.size()]));
                        intent.putExtra(GlobalConstant.KEY_CURRENT_IMAGE_URL, ((ImageAttachment) message.getAttachment()).getThumbUrl());
                        intent.putExtra(PhotoBrowserActivity.KEY_IS_LONG_IMAGE, false);
                        intent.setClass(parent.getContext(), PhotoBrowserActivity.class);
                        parent.getContext().startActivity(intent);
                        ((Activity) parent.getContext()).overridePendingTransition(0, 0);
                    }
                });
            case TITLE_AUDIO:
                return new HWAudioViewHolder(inflater.inflate(R.layout.hw_item_audio, parent, false));
        }
        return null;
    }
    @Override
    public void onBindViewHolder(final HWBaseViewHolder holder, int position) {
        holder.onBindViewHolder(singleList.get(position));
    }

    @Override
    public int getItemCount() {
        return singleList.size();
    }

    @Override
    public void OnCloseClick(int position) {
        singleList.remove(position);
        notifyItemRemoved(position);
    }

    public interface HomeWorkDetaiListener{
        void onPhotoListener(IMMessage message);
    }
}


